package com.dalei.vds.xml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.dalei.vds.model.Group;
import com.dalei.vds.model.Item;
import com.dalei.vds.model.Tab;

public class OutlineItemsXMLReader {

	private InputStream source = null;

	private Document document = null;

	public OutlineItemsXMLReader(InputStream source) {
		// TODO Auto-generated constructor stub
		this.source = source;
	}
	
	public void read() throws DocumentException {
		SAXReader reader = new SAXReader();
		document = reader.read(source);
	}

	public List<Tab> tabs() {
		if (document != null) {
			Element root = document.getRootElement();
			List<Tab> tabs = new ArrayList<Tab>();
			for (Iterator<?> iterator = root.elementIterator(); iterator.hasNext(); ) {
				Element element = (Element) iterator.next();
				Tab tab = new Tab();
				for (Iterator<?> i = element.attributeIterator(); i.hasNext(); ) {
					Attribute attr = (Attribute) i.next();
					String attributeName = attr.getName();
					String attributeValue = attr.getValue();
					if (attributeName.equals("id")) {
						int id = -1;
						try {
							id = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						tab.setId(id);
					} else if (attributeName.equals("name")) {
						tab.setName(attributeValue);
					}
				}
				tabs.add(tab);
			}
			return tabs;
		} else {
			return null;
		}
	}
	
	public Tab getTab(int tabId) {
		if (document != null) {
			Element root = document.getRootElement();
			Element element = getChildElementById(root, tabId);
			if (element != null) {
				Tab tab = new Tab();
				for (Iterator<?> iterator = element.attributeIterator(); iterator.hasNext(); ) {
					Attribute attr = (Attribute) iterator.next();
					String attributeName = attr.getName();
					String attributeValue = attr.getValue();
					if (attributeName.equals("id")) {
						int id = -1;
						try {
							id = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						tab.setId(id);
					} else if (attributeName.equals("name")) {
						tab.setName(attributeValue);
					}
				}
				return tab;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	public List<Group> groups(int tabId) {
		if (document != null) {
			Element root = document.getRootElement();
			Element tab = getChildElementById(root, tabId);
			if (tab != null) {
				List<Group> groups = new ArrayList<Group>();
				for (Iterator<?> iterator = tab.elementIterator(); iterator.hasNext(); ) {
					Element element = (Element) iterator.next();
					Group group = new Group();
					for (Iterator<?> i = element.attributeIterator(); i.hasNext(); ) {
						Attribute attr = (Attribute) i.next();
						String attributeName = attr.getName();
						String attributeValue = attr.getValue();
						if (attributeName.equals("id")) {
							int id = -1;
							try {
								id = Integer.parseInt(attributeValue);
							} catch (NumberFormatException e) {
								// TODO: handle exception
								e.printStackTrace();
								return null;
							}
							group.setId(id);
						} else if (attributeName.equals("name")) {
							group.setName(attributeValue);
						}
					}
					groups.add(group);
				}
				return groups;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	public Group getGroup(int tabId, int groupId) {
		if (document != null) {
			Element root = document.getRootElement();
			Element tab = getChildElementById(root, tabId);
			Element group = getChildElementById(tab, groupId);
			if (group != null) {
				Group g = new Group();
				for (Iterator<?> iterator = group.attributeIterator(); iterator.hasNext(); ) {
					Attribute attr = (Attribute) iterator.next();
					String attributeName = attr.getName();
					String attributeValue = attr.getValue();
					if (attributeName.equals("id")) {
						int id = -1;
						try {
							id = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						g.setId(id);
					} else if (attributeName.equals("name")) {
						g.setName(attributeValue);
					}
				}
				return g;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	public List<Item> items(int tabId, int groupId) {
		if (document != null) {
			Element root = document.getRootElement();
			Element tab = getChildElementById(root, tabId);
			Element group = getChildElementById(tab, groupId);
			if (group != null) {
				List<Item> items = new ArrayList<Item>();
				for (Iterator<?> iterator = group.elementIterator(); iterator.hasNext(); ) {
					Element element = (Element) iterator.next();
					Item item = new Item();
					for (Iterator<?> i = element.attributeIterator(); i.hasNext(); ) {
						Attribute attr = (Attribute) i.next();
						String attributeName = attr.getName();
						String attributeValue = attr.getValue();
						if (attributeName.equals("id")) {
							int id = -1;
							try {
								id = Integer.parseInt(attributeValue);
							} catch (NumberFormatException e) {
								// TODO: handle exception
								e.printStackTrace();
								return null;
							}
							item.setId(id);
						} else if (attributeName.equals("value")) {
							int value = -1;
							try {
								value = Integer.parseInt(attributeValue);
							} catch (NumberFormatException e) {
								// TODO: handle exception
								e.printStackTrace();
								return null;
							}
							item.setValue(value);
						}
					}
					String text = element.getText();
					item.setText(text);
					items.add(item);
				}
				return items;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	public Item getItem(int tabId, int groupId, int itemId) {
		if (document != null) {
			Element root = document.getRootElement();
			Element tab = getChildElementById(root, tabId);
			Element group = getChildElementById(tab, groupId);
			Element item = getChildElementById(group, itemId);
			if (item != null) {
				Item i = new Item();
				for (Iterator<?> iterator = group.attributeIterator(); iterator.hasNext(); ) {
					Attribute attr = (Attribute) iterator.next();
					String attributeName = attr.getName();
					String attributeValue = attr.getValue();
					if (attributeName.equals("id")) {
						int id = -1;
						try {
							id = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						i.setId(id);
					} else if (attributeName.equals("value")) {
						int value = -1;
						try {
							value = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						i.setValue(value);
					}
				}
				String text = item.getText();
				i.setText(text);
				return i;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	private Element getChildElementById(Element parent, int id) {
		if (parent != null) {
			for (Iterator<?> iterator = parent.elementIterator(); iterator
					.hasNext();) {
				Element element = (Element) iterator.next();
				for (Iterator<?> i = element.attributeIterator(); i.hasNext();) {
					Attribute attr = (Attribute) i.next();
					if (attr.getName().equals("id")
							&& attr.getValue().equals(Integer.toString(id))) {
						return element;
					}
				}
			}
			return null;
		} else {
			return null;
		}
	}
}
